package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.Baner.CreadVolumBean;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.adapter.CreadVolumAdapter;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class MyBookComenntActivity extends BaseActivity {

    @BindView(R.id.mycomeent_back)
    ImageView mycomeentBack;
    @BindView(R.id.mybookcomeent_recycle)
    RecyclerView mybookcomeentRecycle;
    private CreadVolumAdapter creadvolumadapter;
    private List<CreadVolumBean.DataBean> list = new ArrayList<>();
    private String bookId;
    private CreadVolumBean cread;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_book_comennt);
        ButterKnife.bind(this);
        bookId=getIntent().getStringExtra("bookId");
        intent=new Intent(this,MyBookChapterActivity.class);
        intView();
        intData();
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/books-getvolume")

                .params("bookId",bookId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        try {
                            cread = gson.fromJson(s, CreadVolumBean.class);
                            if (cread.getReturnCode() == 200) {
                                list.clear();
                                list.addAll(cread.getData());
                                creadvolumadapter.notifyDataSetChanged();
                            } else {
showToast(cread.getMsg());
                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void intView() {
        creadvolumadapter = new CreadVolumAdapter(R.layout.creadvolum_item, list);
        mybookcomeentRecycle.setAdapter(creadvolumadapter);
        mybookcomeentRecycle.setLayoutManager(new LinearLayoutManager(this));
        mybookcomeentRecycle.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        creadvolumadapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(intent);
            }
        });
    }

    @OnClick(R.id.mycomeent_back)
    public void onViewClicked() {
        goBack();
    }
}
